package org.wichit.controller;

import cn.hutool.cache.CacheUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.github.benmanes.caffeine.cache.*;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.wichit.core.request.UserRequest;
import org.wichit.core.response.TokenResponse;
import org.wichit.core.response.UserResponse;
import org.wichit.core.rest.RestHttpService;

import javax.annotation.Resource;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("app")
@Api(value = "登录相关", tags = "小程序登录")
public class UserInfoController {

    @Resource
    private RestHttpService restHttpService;

    Cache<String, Object> cache = Caffeine.newBuilder()
            .expireAfterWrite(15, TimeUnit.MINUTES)
            .maximumSize(1000)
            .build();

    @PostMapping("login")
    @ApiOperation(value = "登录", httpMethod = "POST")
    public Map<String, String> login(@RequestBody UserRequest userRequest) {

        UserResponse userResponse = restHttpService.login(userRequest.getJsCode());
        TokenResponse tokenResponse = restHttpService.getToken();
        cache.put("1",userResponse);

        UserResponse userResponse1 = (UserResponse) cache.getIfPresent("1");
        return null;
    }

}
